package com.vcc.web.controller;


import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.*;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.model.file.comptroller.ComptrollerFilePojo;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.FileUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.core.util.StringUtil;
import com.vcc.data.entity.Comptroller;
import com.vcc.data.entity.IpInfo;
import com.vcc.data.entity.User;
import com.vcc.web.core.util.UserUtil;
import com.vcc.service.client.vo.comptroller.ComptrollerVo;
import com.vcc.service.service.ComptrollerService;
import com.vcc.service.service.IpInfoService;
import com.vcc.service.service.UserService;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("comptroller")
public class ComptrollerController {

    @Autowired
    private ComptrollerService comptrollerService;
    @Autowired
    private UserService userService;
    @Autowired
    private IpInfoService ipInfoService;

    @RestPack
    @GetMapping("paging")
    public Page<ComptrollerVo> paging(
            Page<Comptroller> pageable,
            Integer useFlag,
            String startDate,
            String endDate
    ) {
        Page<Comptroller> paging = comptrollerService.paging(pageable, useFlag, UserUtil.getId(), startDate, endDate);
        return paging.map(it -> {
            String account = userService.getAccountById(it.getUserId());
            String operationUserAccount = userService.getAccountById(it.getOperationUserId());
            IpInfo ipInfo = RegexUtil.checkStringIsNotNull(it.getRequestIp()) ? ipInfoService.getByIpOrNull("0:0:0:0:0:0:0:1".equals(it.getRequestIp()) ? "127.0.0.1" : it.getRequestIp()) : null;
            return new ComptrollerVo(it, account, operationUserAccount, ipInfo);
        });
    }


    @RestPack
    @GetMapping("derive")
    public void derive(
            @CurrentROStatus Boolean roStatus,
            Integer useFlag,
            String startDate,
            String endDate,
            HttpServletResponse response
    ) {
        if (RegexUtil.checkStringIsNull(startDate)) {
            throw new ProgramException("请选择开始时间");
        }
        if (RegexUtil.checkStringIsNull(endDate)) {
            throw new ProgramException("请选择结束时间");
        }
        Long pages = 1L;
        Page<Comptroller> pageable;
        SXSSFWorkbook sxssfWorkbook = FileUtil.createComptrollerTitle(10000);
        Page<Comptroller> paging;
        do {
            pageable = new Page<>(pages++, 20);
            paging = comptrollerService.paging(pageable, useFlag, UserUtil.getId(), startDate, endDate);
            List<ComptrollerFilePojo> list = paging.getRecords().stream().map(it -> {
                String account = userService.getAccountById(it.getUserId());
                String operationUserAccount = userService.getAccountById(it.getOperationUserId());
                return new ComptrollerFilePojo(StringUtil.hideMobile(account), it.getUseFlag(), it.getStatus(), it.getRequestIp(), StringUtil.hideMobile(operationUserAccount), DateUtil.format(it.getRequestDate()));
            }).collect(Collectors.toList());
            FileUtil.deriveComptrollerData(sxssfWorkbook, list, paging.getPageNumber(), paging.getPageSize(), roStatus);
        } while (!(pages).equals(paging.getTotalPage() + 1));
        String fileName = System.currentTimeMillis() + "用户审计详情.xlsx";

            FileUtil.downXlsxFile(response, sxssfWorkbook, fileName);

    }
}
